Skip to content

Conversation

@qianheng-aws
Copy link
Collaborator

Description

SUM aggregation enhancement on operations with literal

Related Issues

Resolves #3967

Check List

  • New functionality includes testing.
  • New functionality has been documented.
  • New functionality has javadoc added.
  • New functionality has a user manual doc added.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff.
  • Public documentation issue/PR created.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Signed-off-by: Heng Qian <qianheng@amazon.com>
Signed-off-by: Heng Qian <qianheng@amazon.com>
Signed-off-by: Heng Qian <qianheng@amazon.com>
/**
* Ignore queries that are not supported by Calcite. Ignore q30 because of too much script push
* down, which will cause ResourceMonitor restriction.
* Ignore queries that are not supported by Calcite. TODO: need to investigate on why q30 trigger
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calcite will produce lots of recyclable memory garbage during its planning and implementation process. For q30, it produces around 70 mb in optimizing + 30 mb in implementation on my local test, which can all be GC.

So I think, for Calcite, the ResourceMonitor's memory health check is unpredictable and less reasonable.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ResourceMonitor protect physical operator, how it related to Calcite optimizing/implementation logica?

Copy link
Collaborator Author

@qianheng-aws qianheng-aws Aug 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static class MemoryUsage {
public long usage() {
final long freeMemory = Runtime.getRuntime().freeMemory();
final long totalMemory = Runtime.getRuntime().totalMemory();
return totalMemory - freeMemory;
}

ResourceMonitor get the memory usage by using total memory - free memory. While the whole process of a query(even other queries previous to the current one) will accumulate the memory usage until GC.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've submitted a PR #3983 to fix the issue


/** See source in {@link org.apache.calcite.rel.core.Aggregate::computeSelfCost} */
private static float getAggMultiplier(PushDownAction action) {
List<AggregateCall> aggCalls = ((Aggregate) action.digest).getAggCallList();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a comments before and after code from calcite

// START CALCITE
...
your code
// END CALCITE

// For script aggregation, we need to multiply the multiplier by 2.2 to make up the cost. As we
// prefer to have non-script agg push down after optimized by {@link PPLAggregateConvertRule}
if (((AggPushDownAction) action.action).isScriptPushed) {
multiplier *= 2.2f;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2.2f magic number, Can i be changed in future?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually it doesn't need any change unless we met some edge case(like the final plan doesn't satisfy our expectation) where I need refactor it.

Similar to the factor we add in filter script push down:

/**
* Ignore queries that are not supported by Calcite. Ignore q30 because of too much script push
* down, which will cause ResourceMonitor restriction.
* Ignore queries that are not supported by Calcite. TODO: need to investigate on why q30 trigger
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ResourceMonitor protect physical operator, how it related to Calcite optimizing/implementation logica?

import org.immutables.value.Value;

/**
* Planner rule that converts specific aggCall to a more efficient expressions, which includes: -
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add UT

Comment on lines 41 to 42
* NUMBER, Don't support this because of integer division - MAX/MIN(FIELD [+|-|*|+|/] NUMBER) ->
* MAX/MIN(FIELD) [+|-|*|+|/] NUMBER
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

format java doc.

/**
* Planner rule that converts specific aggCall to a more efficient expressions, which includes: -
* SUM(FIELD + NUMBER) -> SUM(FIELD) + NUMBER * COUNT() - SUM(FIELD - NUMBER) -> SUM(FIELD) - NUMBER
* * COUNT() - SUM(FIELD * NUMBER) -> SUM(FIELD) * NUMBER - SUM(FIELD / NUMBER) -> SUM(FIELD) /
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SUM(FILED / NUMBER) -> SUM(FIELD) / NUMBER, does it work for float/double?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, they also have precision issue, e.g:

assuming SUM(a / 0.2) and the table has a = 0.1 for its all 3 rows. The execution could be present:

0.1 / 0.2 * 3 = 1.5

0.1 * 3 / 0.2 = 1.5000000000000002

Signed-off-by: Heng Qian <qianheng@amazon.com>
Signed-off-by: Heng Qian <qianheng@amazon.com>
qianheng-aws and others added 4 commits August 15, 2025 11:18
# Conflicts:
#	integ-test/src/test/java/org/opensearch/sql/calcite/remote/CalciteExplainIT.java
Signed-off-by: Heng Qian <qianheng@amazon.com>
# Conflicts:
#	integ-test/src/test/java/org/opensearch/sql/calcite/remote/CalciteExplainIT.java
@qianheng-aws
Copy link
Collaborator Author

@penghuo Please take another look.

@penghuo penghuo merged commit 8923551 into opensearch-project:main Aug 21, 2025
23 checks passed
@opensearch-trigger-bot
Copy link
Contributor

The backport to 2.19-dev failed:

The process '/usr/bin/git' failed with exit code 128

To backport manually, run these commands in your terminal:

# Navigate to the root of your repository
cd $(git rev-parse --show-toplevel)
# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add ../.worktrees/sql/backport-2.19-dev 2.19-dev
# Navigate to the new working tree
pushd ../.worktrees/sql/backport-2.19-dev
# Create a new branch
git switch --create backport/backport-3971-to-2.19-dev
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 892355190687033b0f29bae624182c1ba204bf96
# Push it to GitHub
git push --set-upstream origin backport/backport-3971-to-2.19-dev
# Go back to the original working tree
popd
# Delete the working tree
git worktree remove ../.worktrees/sql/backport-2.19-dev

Then, create a pull request where the base branch is 2.19-dev and the compare/head branch is backport/backport-3971-to-2.19-dev.

qianheng-aws added a commit to qianheng-aws/sql that referenced this pull request Aug 22, 2025
…oject#3971)

* SUM aggregation enhancement on operations with literal

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Fix CI

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Keep ignoring q30 for Calcite

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Add UT for PPLAggregateConvertRule

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Add UT for PPLAggregateConvertRule

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Spotless check

Signed-off-by: Heng Qian <qianheng@amazon.com>

---------

Signed-off-by: Heng Qian <qianheng@amazon.com>
Co-authored-by: Mitchell Gale <mitchell.gale@improving.com>
(cherry picked from commit 8923551)
LantaoJin pushed a commit that referenced this pull request Aug 25, 2025
…teral (#3971) (#4104)

* SUM aggregation enhancement on operations with literal (#3971)

* SUM aggregation enhancement on operations with literal

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Fix CI

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Keep ignoring q30 for Calcite

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Add UT for PPLAggregateConvertRule

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Add UT for PPLAggregateConvertRule

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Spotless check

Signed-off-by: Heng Qian <qianheng@amazon.com>

---------

Signed-off-by: Heng Qian <qianheng@amazon.com>
Co-authored-by: Mitchell Gale <mitchell.gale@improving.com>
(cherry picked from commit 8923551)

* Fix Compiling

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Fix Compiling

Signed-off-by: Heng Qian <qianheng@amazon.com>

* Fix Compiling

Signed-off-by: Heng Qian <qianheng@amazon.com>

---------

Signed-off-by: Heng Qian <qianheng@amazon.com>
Co-authored-by: Mitchell Gale <mitchell.gale@improving.com>
Co-authored-by: MitchellGale-BitQuill <104795536+mitchellgale-bitquill@users.noreply.github.com>
@LantaoJin LantaoJin added the backport-manually Filed a PR to backport manually. label Sep 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport 2.19-dev backport-failed backport-manually Filed a PR to backport manually. calcite calcite migration releated enhancement New feature or request PPL Piped processing language

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Aggregation enhancement for SUM on FIELD + NUMBER

4 participants